#include <simulatorV1/instruction/mv.h>

bool MV::checkFormat(std::shared_ptr<FunctionNode> functionNode) {
    if (functionNode->m_parameters.size() != 2) {
        return false;
    }
    if (functionNode->m_funcName != "MV" && functionNode->m_funcName != "mv") {
        return false;
    }

    // mv src,dst
    auto para1 = functionNode->m_parameters[0];
    auto para2 = functionNode->m_parameters[1];
    if (std::dynamic_pointer_cast<RegNode>(para1) != nullptr
        && std::dynamic_pointer_cast<RegNode>(para2) != nullptr) {
        m_instFormat = InstFormat::Kind1;
        return true;
    }
    return false;
}

void MV::generateIR(std::shared_ptr<FunctionNode> functionNode, llvm::Module *module,
                    std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    if (m_instFormat == InstFormat::Kind1) {
        auto para1 = functionNode->m_parameters[0];
        auto srcRegNode = std::dynamic_pointer_cast<RegNode>(para1);
        auto para2 = functionNode->m_parameters[1];
        auto dstRegNode = std::dynamic_pointer_cast<RegNode>(para2);

        auto dstRegPtr = module->getNamedGlobal(TagName[static_cast<u32>(dstRegNode->m_regKind)]);
        auto srcRegPtr = module->getNamedGlobal(TagName[static_cast<u32>(srcRegNode->m_regKind)]);
        irBuilder->CreateStore(irBuilder->CreateLoad(srcRegPtr), dstRegPtr);
    }
}

std::vector<std::string> MV::formatToString() {
    std::vector<std::string> formatStr;
    formatStr.emplace_back("MV src,dst");
    return std::move(formatStr);
}